/*********************************************************************
 *	CGE Model (Center - regions) 
 *
 *	Authors: 
 *		D.B. Nurseitov: C++ code from Pascal code, optimization, MPI.
 *		N.Yu. Borowski: Pascal code, model realization, Nelder-Mid.
 *	Created: 07.11.2010
 *
 *	FIXES:
 *		11.11.2010:	Bcast leader Const.txt file for next series.
 *		15.11.2010:	Stopping rule using time limit.
 *		16.11.2010:	Locale, Y__2 limitation.
 *		24.11.2010:	fix of unsafe sprintf to snprintf; fixed double Stat, Weight underful fp error.
 *		11.02.2011:	fix for NTU cluster.
 *		03.03.2011:	regression added.
 *		29.04.2011:	sensitivity added.
 *
 ********************************************************************/
#include "CGEDefs.h"
CGEException::CGEException(const cstring & msg) : _msg(msg){};
    
CGEException::CGEException(const char * pchMsg)
    {
	_msg = pchMsg;
    }
    
CGEException::~CGEException() throw() {};
    
const char * CGEException::what() const throw() {return _msg.c_str(); }

int TStringList::IndexOf(const cstring & str) const
    {
	int idx = 0;
		
	for(TStringList::const_iterator it = begin(); it != end(); it++, idx++)
	{
	    if(!strcmp(it->c_str(), str.c_str()))
		return idx;
	}
	return -1;
    }

// TConstFileArray = Array [1..4] of String;
TConstFileArray::TConstFileArray() : std::vector<cstring>(5){};

// TStatFileArray = Array [1..17] of String;
TStatFileArray::TStatFileArray() : std::vector<cstring>(18){};

// TCIN = array [1..MaxCNum] of Double;
TCIN::TCIN() : std::vector<Real>(MaxCNum+1){};
	
Extended TCIN::Norm() const
    {
	Extended norm = 0;
	for(int i = 0; i <= MaxCNum; i++)
	{
	    norm += this->at(i);
	}
	return norm;
    };

//friend std::ostream & operator<<(std::ostream & s, const TCIN & a);

// TCINIdent = array [1..MaxIdentI] of Double;
TCINIdent::TCINIdent() : std::vector<Real>(MaxIdentI+1){};

// TCidentI = Array [1..MaxIdentI+1] of Integer;
TCidentI::TCidentI() : std::vector<int>(MaxIdentI+2){};

// TCi = array [1..16] of Double;
TCi::TCi() : std::vector<Real>(17){};

// TCij = array [1..16, 1..16] of Double;
TCij::TCij() : std::vector<TCi>(17){};

//  TVijArray = array [0..16] of TCij;
TVijArray::TVijArray() : std::vector<TCij>(17){};

// TCiArray = array [0..15] of TCi;
TCiArray::TCiArray() : std::vector<TCi>(16){};

// TCArray = array [0..15] of Double;
TCArray::TCArray() : std::vector<Real>(16){};

// TViArray = array [0..16] of TCi;
TViArray::TViArray() : std::vector<TCi>(17){};

TVArray::TVArray() : std::vector<Real>(17){};

// TVIN = array [1..MaxVNum] of Double;
TVIN::TVIN() : std::vector<Real>(MaxVNum+1){};

// TVOUT = array [0..15] of TVIN;
TVOUT::TVOUT() : std::vector<TVIN>(16){};

std::ostream & operator<<(std::ostream & s, const TVOUT & a)
{
	for(int i = 1; i <= MaxVNum; i++)
	{
		for(int j = 0; j <= 15; j++)
		{
			s << a[j][i] << "\t";
		}
		s << std::endl;
	}
	/*for(TVOUT::const_iterator it1 = a.begin(); it1 != a.end(); it1++)
	{
		for(TVIN::const_iterator it2 = it1->begin(); it2 != it1->end(); it2++)
		{
			s << *it2 << "\t";
		}
		s << std::endl;
	}*/
	return s;
}

// TV_star = array [0..15] of Double;
TV_star::TV_star() : std::vector<Real>(16){};

// TVSTAT = array [0..15] of TVIN;
TVSTAT::TVSTAT() : std::vector<TVIN>(16){};

// CRec : Array [1..MaxCNum] of TCRec;
CRecArray::CRecArray() : std::vector<TCRec>(MaxCNum+1){};
		
//friend std::ostream & operator<<(std::ostream & s, const CRecArray & a);

std::ostream & operator<<(std::ostream & s, const TCIN & a)
{
	for(TCIN::const_iterator it = a.begin(); it != a.end(); it++)
	{
		s << *it << std::endl;
	}
	return s;
}

std::ostream & operator<<(std::ostream & s, const CRecArray & a)
{
	for(CRecArray::const_iterator it = a.begin(); it != a.end(); it++)
	{
		s << it->Name.c_str() << "\t" << it->C << "\t" << it->Cmin << "\t" << it->Cmax << "\t" << it->ident << std::endl;
	}
	return s;
}

// Vrec : Array [1..MaxVNum] of TVRec;
VRecArray::VRecArray() : std::vector<TVRec>(MaxVNum+1){};

std::ostream & operator<<(std::ostream & s, const VRecArray & a)
{
	for(int i = 1; i <= MaxVNum; i++)
	{
		s << a[i].Name.c_str() << "\t";
	}
	s << std::endl;
	return s;
}

// Daniyar.
// Class for correct outputing data.
// Variable name + tab + var0 + tab + var1 + ... + var15 + endl.
std::ostream & operator<<(std::ostream & s, const VRecTVOUT & a)
{
	for(int i = 1; i <= MaxVNum; i++)
	{
		s << a._vr[i].Name.c_str();
		for(int j = 0; j <= 15; j++)
		{
			s << "\t" << a._tv[j][i];
		}
		s << std::endl;
	}
	return s;
}

void MsgErr(cstring msg){ std::cout << "Error at line(" << __LINE__ << "):" << msg << std::endl;}
void MsgInf(cstring msg){ std::cout << "Info at line(" << __LINE__ << "):" << msg << std::endl;}
